package com.zp.datax.core.function;

import java.util.function.Function;

/**
 * Created by bruce on 2018/12/18.
 */
@FunctionalInterface
public interface FunctionWrapper<T, R, E extends Exception> {

    R apply(T t) throws E;

    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        return v -> {
            try {
                return apply(before.apply(v));
            } catch (Exception e) {
                throw new FunctionException(e);
            }
        };

    }

    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        return t -> {
            try {
                return after.apply(apply(t));
            } catch (Exception e) {
                throw new FunctionException(e);
            }
        };

    }

}
